package phoenics.apeak.crawl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;


public class CookieTools {
	private static  Map<String, String> map = System.getenv();  
	private static InputStreamReader takeDocument(String codejs,String url)throws IOException {
		Runtime rt = Runtime.getRuntime();
		Process p = rt.exec(getHantomjsExe()+" "+getHantomjslib().trim()+codejs.trim()+" " + url);// 这里我的codes.js是保存在c盘下面的phantomjs目录
		InputStream is = p.getInputStream();
		return new InputStreamReader(is);
	}
	public static String getHantomjsExe()  {
		return map.get("phantomjs_home")+"/bin/phantomjs";
	}
	public static String getHantomjslib()  {
		return map.get("phantomjs_home")+"/lib/";
	}
	public static String retrieval(BufferedReader br,String feature)throws IOException {
		StringBuffer sbf = new StringBuffer();
		String tmp = ""; 
		while ((tmp = br.readLine()) != null) {
			if(tmp.startsWith(feature)){
				sbf.append(tmp.replace(feature, ""));
			}
		}
		br.close();
		return sbf.toString();
	}
	public static String takeCookies(String codejs,String url) throws IOException {
		return retrieval(new BufferedReader(takeDocument(codejs,url)),"#1#cookies#1#");
	}
	

}
